package com.veetao.api.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang3.time.DateUtils;

public class DateUtil {

    public static final String DATE_TIME = "yyyy-MM-dd";

    public static final String DATE_TIME_SS = "yyyy-MM-dd HH:mm:ss";

    private static String defaultTimeZoneStr = "Asia/Shanghai";

    public static String dateToDb(Date date) {
        return dateToStr(date, DATE_TIME_SS);
    }

    public static String getDateFormat() {
        return dateTimeToStr(new Date());
    }

    public static String getDateFormat(String time) {
        Date stringToDateTime = StringToDateTime(time);
        return dateTimeToStr(stringToDateTime);
    }

    public static long getTimeMillisForDateFormat(String time) {
        SimpleDateFormat dateFormate = new SimpleDateFormat(DATE_TIME_SS);
        try {
            Date parse = dateFormate.parse(time);
            return parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getDateFormdd() {
        
        return dateToStr(new Date());
    }

    public static String dateToStr(Date date) {

        return dateToStr(date, DATE_TIME);
    }

    public static String dateTimeToStr(Date date) {

        return dateToStr(date, DATE_TIME_SS);
    }

    public static int getHours(String timeZoneStr) {
        Calendar s = Calendar.getInstance();
        if (timeZoneStr == null || "".equals(timeZoneStr)) {
            timeZoneStr = defaultTimeZoneStr;
        }
        TimeZone timeZone = TimeZone.getTimeZone(timeZoneStr);
        s.setTimeZone(timeZone);
        return s.get(Calendar.HOUR_OF_DAY);
    }

    public static Date StringToDateTime(String time) {
        Date result = null;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            result = df.parse(time);
        } catch (ParseException pe) {
            pe.printStackTrace();
        }
        return result;
    }

    public static Date StringToDate(String time) {
        Date result = null;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            result = df.parse(time);
        } catch (ParseException pe) {
            pe.printStackTrace();
        }
        return result;
    }

    public static String dateToStrWithUTCTimeZone(Date dt, String format) {
        return dateToStrWithTimeZone(dt, format, TimeZone.getTimeZone("UTC"));
    }

    public static String dateToStrWithUTCTimeZone(Date date) {
        return dateToStrWithTimeZone(date, "MM/dd/yyyy, HH:mm", TimeZone.getTimeZone("UTC")) + " UTC";
    }

    public static String dateToStr(Date date, String format) {
        return dateToStrWithTimeZone(date, format, null);
    }

    public static String dateToStrWithTimeZone(Date date, String format, TimeZone tz) {

        String dateStr = "";
        try {

            SimpleDateFormat dateFormate = new SimpleDateFormat(format);
            if (tz != null) {
                dateFormate.setTimeZone(tz);
            }
            dateStr = dateFormate.format(date);

        } catch (Exception e) {
            System.err.println(e);
        }
        return dateStr;
    }

    public static Date parseDate(String timeStr, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(timeStr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    public static Date parseDate(String timeStr, String format, Locale loc) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, loc);
        try {
            return dateFormat.parse(timeStr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    public static String createPublishTimeNice(Date date) {
        long createMS = date.getTime();
        long now = System.currentTimeMillis();

        int ms5minutes = 300000;
        int ms1hour = 3600000;
        long todayPassedMS = now % 86400000;

        long timeDiff = now - createMS;

        if (timeDiff < ms5minutes) {
            return "刚刚";
        }

        if (timeDiff < ms1hour) {
            return (timeDiff / 60000) + " 分钟前";
        }

        if (timeDiff < todayPassedMS) {
            return (timeDiff / 3600000) + " 小时前";
        }

        return DateUtil.dateToStr(date, "yyyy-MM-dd HH:mm");

    }

    public static void main(String[] args) {

        System.out.println(getRemainingSecondsToday());
        Calendar c2 = Calendar.getInstance();
        c2.set(Calendar.HOUR_OF_DAY, 24);
        c2.set(Calendar.MINUTE, 0);
        c2.set(Calendar.SECOND, 0);
        long seconds = c2.getTimeInMillis() - System.currentTimeMillis();
        System.out.println(seconds);

        Date d = new Date();
        String s = createPublishTimeNice(d);

        System.out.println(s);

        String timenxb = "2018-04-25 00:00:00";
        Date parseDate = parseDate(timenxb, "yyyy-MM-dd HH:mm:ss");
        System.out.println(parseDate.getTime());

        d = new Date();
        System.out.println(".....");
        System.out.println(StringToDateTime("2015-08-20 23:20:00"));
        System.out.println(dateToStr(d, "yyyyMMddHHmmss"));
        System.out.println(dateToStr(d, "yyyyMMddHHmmSSS"));
        getTodayStartTimeInSeconds();

        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        // c.setTimeInMillis(d.getTime());
        d = c.getTime();
        System.out.println(c.getTimeZone().getDisplayName());
        System.out.println(dateToStr(d, "yyyyMMddHHmmss"));

        c = Calendar.getInstance();
        d = c.getTime();
        System.out.println(c.getTimeZone().getDisplayName());
        System.out.println(dateToStr(d, "yyyyMMddHHmmss"));

        SimpleDateFormat dateFormate = new SimpleDateFormat("yyyyMMddHHmmss");
        dateFormate.setTimeZone(TimeZone.getTimeZone("UTC"));
        System.out.println(dateFormate.format(d));
    }

    public static boolean isSameDay(Date d1, Date d2) {
        return DateUtils.isSameDay(d1, d2);
    }

    public static Date addDays(Date d, int i) {
        return DateUtils.addDays(d, i);
    }

    /**
     * @return
     * @author guohongtao
     */

    public static long getTodayStartTimeInSeconds() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);

        return c.getTimeInMillis() / 1000;

    }

    /**
     * 获取今日剩余秒数，作为存储每日登陆的时间
     * 
     * @return
     */
    public static long getRemainingSecondsToday() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 24);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return (c.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }

    // public static long getTodayTimeInSeconds() {
    //
    // }

    public static Date getUTCDate() {
        java.util.TimeZone zone = java.util.TimeZone.getTimeZone("GMT-0:00");
        java.util.Calendar cal = java.util.Calendar.getInstance(zone);
        return cal.getTime();
    }

}
